<script lang="ts" setup>
import { ref, reactive, onBeforeMount } from "vue";
import { FormInstance, FormRules, ElMessage } from "element-plus";
import mainEmit from "@/utils/bus";
import { LoginApi } from "@/Api";

const formRef = ref<FormInstance>();

const formData = reactive({
   account: "",
   pwd: ""
});

const rules = reactive<FormRules>({
   account: [
      { required: true, message: "请输入账号/手机号码", trigger: "blur" },
      { min: 5, max: 20, message: "长度在 5 - 20 之间", trigger: "blur" }
   ],
   pwd: [
      { required: true, message: "请输入密码", trigger: "blur" },
      { min: 8, max: 16, message: "长度在 5 - 20 之间", trigger: "blur" }
   ]
});

const loading = reactive({ submit: false });
const disable = reactive({ submit: false });

// 登录
function submit(formRef?: FormInstance) {
   if (!formRef) return;
   loading.submit = true;
   disable.submit = true;

   formRef.validate(async (valid, fields) => {
      if (!valid) return;

      const { data, code } = await LoginApi.Password(formData);
      if (code === 200) {
         mainEmit.emit("loginResult", data);
      }
   });

   loading.submit = false;
   disable.submit = false;
}

onBeforeMount(() => {
   const { VITE_NODE_ENV } = import.meta.env;

   if (VITE_NODE_ENV === "development") {
      formData.account = "15556359657";
      formData.pwd = "jc888888";
   }
});
</script>

<template>
   <el-form ref="formRef" class="elForm" :model="formData" :rules="rules" label-width="auto">
      <el-form-item prop="account">
         <template v-slot:label>
            <span class="label">账号</span>
         </template>
         <el-input v-model="formData.account" placeholder="请输入账号或手机号码" />
      </el-form-item>
      <el-form-item prop="pwd">
         <template v-slot:label>
            <span class="label">密码</span>
         </template>
         <el-input
            v-model="formData.pwd"
            type="password"
            @keyup.enter="submit"
            placeholder="请输入密码"
            show-password></el-input>
      </el-form-item>
      <el-form-item class="elButton">
         <el-button @click="submit(formRef)">登录</el-button>
      </el-form-item>
   </el-form>
</template>

<style lang="scss" scoped>
.elForm {
   width: 100%;
   :deep(.el-input) {
      background-color: transparent;
      .el-input__wrapper {
         background-color: rgba($color: #fff, $alpha: 0.1);
         transition: all 0.3s;
         // border: 1px solid rgba($color: #fff, $alpha: 0.5);
         box-shadow: 0 0 0 1px rgba($color: #fff, $alpha: 0.3) inset;

         .el-input__inner {
            background-color: transparent;
            color: #000;
         }
      }
      .is-focus {
         background-color: rgba($color: #fff, $alpha: 0.5);
      }
   }

   :deep(.send) {
      .el-input-group__append {
         width: 20px;
         background-color: rgba($color: #fff, $alpha: 0.1);
         border: 1px solid rgba($color: #fff, $alpha: 0.3);
         border-left: none;
         color: #fff;
         transition: all 0.3s;
         box-shadow: none;

         &:hover {
            background-color: rgba($color: #fff, $alpha: 0.3);
            border: 1px solid rgba($color: #fff, $alpha: 0.5);
            border-left: none;
         }
         .is-loading {
            > span {
               display: none;
            }
            &::before {
               background-color: transparent;
            }
         }
      }
   }
}
.label {
   color: #fff;
}

.elButton {
   :deep(.el-button) {
      margin-top: 30px;
      width: 100%;
      background-color: transparent;
      border: 1px solid rgba($color: #fff, $alpha: 0.3);
      color: #fff;
      transition: all 0.3s;

      &:hover {
         background-color: rgba($color: #fff, $alpha: 0.5);
         color: #000;
      }
   }
}
</style>
